﻿#include <iostream>
#include <thread>
#include <math.h>

#include <zmq.hpp>
#include <spdlog/spdlog.h>

int main()
{
	zmq::context_t ctx;
	zmq::socket_t subscirber = zmq::socket_t(ctx, ZMQ_SUB);
	subscirber.connect("tcp://localhost:5556");

	std::string filter = "1001";
	subscirber.setsockopt(ZMQ_SUBSCRIBE, filter.data(), filter.size());

	long total_temp = 0;
	for (int i = 0; i < 100; ++i)
	{
		zmq::message_t msg;
		subscirber.recv(&msg);

		std::string msg_str((char*)msg.data(), msg.size());
		std::stringstream is(msg_str);
		int zipcode, temperature, relhumidity;
		is >> zipcode >> temperature >> relhumidity;
		total_temp += temperature;

		spdlog::info("{} temperature: {}", zipcode, temperature);
	}

	spdlog::info("Average temperature: {}", total_temp / 100);

	int tmp;
	std::cin >> tmp;
}